package com.ahtcm.tcmlearning.service.iml;

import com.ahtcm.tcmlearning.dao.MedicineMapper;
import com.ahtcm.tcmlearning.dao.PrescriptionMapper;
import com.ahtcm.tcmlearning.dao.QuestionRecordMapper;
import com.ahtcm.tcmlearning.dataObject.brushProblem.RateOfLearning;
import com.ahtcm.tcmlearning.dataObject.generateQuestion.GenerateQuestion;
import com.ahtcm.tcmlearning.enums.QuestionRecordTypeEnum;
import com.ahtcm.tcmlearning.model.Medicine;
import com.ahtcm.tcmlearning.model.Prescription;
import com.ahtcm.tcmlearning.model.QuestionRecord;
import com.ahtcm.tcmlearning.service.GenerateProblemService;
import com.ahtcm.tcmlearning.utils.brushProblem.RateUtil;
import com.ahtcm.tcmlearning.utils.generateQustion.MedicineQuestionGenerateUtil;
import com.ahtcm.tcmlearning.utils.generateQustion.PrescriptionQuestionGenerateUtil;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class GenerateProblemServiceIml implements GenerateProblemService {
    private static final int isValid = 1;

    @Autowired
    QuestionRecordMapper questionRecordMapper;
    @Autowired
    MedicineMapper medicineMapper;
    @Autowired
    PrescriptionMapper prescriptionMapper;

    //获得用户记忆斩进度
    @Override
    public RateOfLearning getRateOfLearning(Integer functionType, Integer userId) {
        QuestionRecord userDegree = questionRecordMapper.findContentIdByQuestionType(functionType, userId);
        List table = findTable(functionType);
        System.out.println("table:" + table);
        if (table == null || table.size() == 0) return new RateOfLearning(0, 0, "该模块未开启，敬请期待！",null);
        if (userDegree == null) return new RateOfLearning(0, 0, "0%",null);
        return new RateOfLearning(table.size(), userDegree.getContentId(), RateUtil.learningRate(table.size(), userDegree.getContentId()),userDegree.getDicId());

    }

    private List findTable(Integer functionType) {
        //搜索中药库
        if (functionType.equals(QuestionRecordTypeEnum.MEDICINE_MODULAR__RECORD.getCode())) {
            List<Medicine> list = medicineMapper.selectAllFromMedicine();
            System.out.println("中药模块");
            return list;
        }
        //搜索方剂库
        if (functionType.equals(QuestionRecordTypeEnum.PRESCRIPTION.getCode())) {
            List<Prescription> list = prescriptionMapper.selectAllFromPrescription();
            System.out.println("方剂模块");
            return list;
        }
        return null;

    }

    @Override
    public Medicine getNextTypeDetailsForMedicine(Integer currentTypeId) {
        List<Medicine> medicineList = medicineMapper.selectAllFromMedicine();
        int nextIndex = 0;
        for (Medicine medicine : medicineList) {
            if (medicine.getMedicineId().equals(currentTypeId)) {
                System.out.println(medicineList.indexOf(medicine) + 1);
                nextIndex = medicineList.indexOf(medicine) + 1;
            }
        }
        if (nextIndex > (medicineList.size() - 1))
            return medicineList.get(0);
        return medicineList.get(nextIndex);
    }

    @Override
    public Prescription getNextTypeDetailsForPrescription(Integer currentTypeId) {
        List<Prescription> prescriptionList = prescriptionMapper.selectAllFromPrescription();
        int nextIndex = 0;
        for (Prescription prescription : prescriptionList) {
            if (prescription.getPrescriptionId().equals(currentTypeId)) {
                System.out.println(prescriptionList.indexOf(prescription) + 1);
                nextIndex = prescriptionList.indexOf(prescription) + 1;
            }
        }
        if (nextIndex > (prescriptionList.size() - 1))
            return prescriptionList.get(0);
        return prescriptionList.get(nextIndex);
    }

    @Override
    @Transactional
    public void skipOverTopic(Integer userId, Integer typeId, Integer dicId, Integer functionType) {
        Date createTime = new Date();
        questionRecordMapper.addQuestionRecord(userId, null, null, createTime, isValid, dicId, functionType, typeId);
    }

    @Override
    public List<GenerateQuestion> generateMedicineQuestion(Integer typeId, Integer dicId) {
        List<Medicine> medicineList=medicineMapper.selectAllFromMedicine();
        List<Medicine> waitingOption=new ArrayList<>() ;
        List<Medicine> otherOption=new ArrayList<>();
        //包含正确和错误的四个中药选项
        List<Medicine> optionMedicine=new ArrayList<>();
        //正确答案
        Medicine rightOption=new Medicine();
        for (Medicine medicine:medicineList){
            if (medicine.getDicId().equals(dicId)&&!medicine.getMedicineId().equals(typeId))
                waitingOption.add(medicine);
            if (medicine.getMedicineId().equals(typeId))
                rightOption=medicine;
        }
        if (waitingOption.size()>3){
            Collections.shuffle(waitingOption);
            otherOption=waitingOption.subList(0,3);
        }else {
            otherOption=waitingOption;
        }
        optionMedicine=otherOption;
        optionMedicine.add(rightOption);

      List<GenerateQuestion> list= MedicineQuestionGenerateUtil.generate(optionMedicine,rightOption);
        System.out.println("中药题目生成完毕");
        return list;
    }

    @Override
    public List<GenerateQuestion> generatePrescriptionQuestion(Integer typeId, Integer dicId) {
        List<Prescription> prescriptionList=prescriptionMapper.selectAllFromPrescription();
        List<Prescription> waitingOption=new ArrayList<>() ;
        List<Prescription> otherOption=new ArrayList<>();
        //包含正确和错误的四个方剂选项
        List<Prescription> optionPrescription=new ArrayList<>();
        //正确答案
        Prescription rightOption=new Prescription();
        for (Prescription prescription:prescriptionList){
            if (prescription.getDicId().equals(dicId)&&!prescription.getPrescriptionId().equals(typeId))
                waitingOption.add(prescription);
            if (prescription.getPrescriptionId().equals(typeId))
                rightOption=prescription;
        }
        if (waitingOption.size()>3){
            Collections.shuffle(waitingOption);
            otherOption=waitingOption.subList(0,3);
        }else {
            otherOption=waitingOption;
        }
        optionPrescription=otherOption;
        optionPrescription.add(rightOption);

        List<GenerateQuestion> list= PrescriptionQuestionGenerateUtil.generate(optionPrescription,rightOption);
        System.out.println("方剂题目生成完毕");
        return list;
    }


}